import {useEffect} from "react"
import { useLocation,useNavigate } from "react-router-dom"
import useGetUserInfo from "./useGetUserInfo"
function isLoginOrRegister(pathname:string){
    if(["/login","/register"].includes(pathname)) return true
    return false
}
function isNoNeedUserInfo(pathname:string){
    if(["/login","/register","/"].includes(pathname)) return true
    return false
}
function useNavPage(waitingUserData:boolean){
    const {username}=useGetUserInfo()
    const {pathname}=useLocation()
    const nav=useNavigate()
    useEffect(()=>{
        if(waitingUserData) return
        //如果登录
        if(username){
            if(isLoginOrRegister(pathname)){
                nav("/manage/list")
            }
            return
        }
        //未登录
        if(isNoNeedUserInfo(pathname)){
            return
        }else{
            nav('/login')
        }
    },[username,pathname,waitingUserData])

}
export default useNavPage